package com.yuandian.management.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuandian.api.management.entity.ConfComponentIp;
import com.yuandian.api.management.entity.ConfIpDriftOnline;
import com.yuandian.api.management.entity.ConfSegment;
import com.yuandian.api.management.entity.ConfSystem;
import com.yuandian.management.mapper.ConfIpDriftOnlineMapper;
import com.yuandian.management.service.ConfComponentIpService;
import com.yuandian.management.service.ConfIpDriftOnlineService;
import com.yuandian.management.service.ConfSegmentService;
import com.yuandian.management.service.ConfSystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 漂移IP上线表-Service实现
 * @author EDY
 * @date 2023-3-15
 */
@Service
public class ConfIpDriftOnlineServiceImpl extends ServiceImpl<ConfIpDriftOnlineMapper, ConfIpDriftOnline>
		implements ConfIpDriftOnlineService {

	@Autowired
	private ConfComponentIpService confComponentIpService;

	@Autowired
	private ConfSegmentService confSegmentService;

	@Autowired
	private ConfIpDriftOnlineMapper confIpDriftOnlineMapper;

	@Autowired
	private ConfSystemService confSystemService;

	/**
	 * 重写分页方法
	 * @param confIpDriftOnlineParams 过滤对象
	 * @return 通用返回对象
	 */
	@Override
	public Page<ConfIpDriftOnline> listPage(ConfIpDriftOnline confIpDriftOnlineParams) {
		Page<ConfIpDriftOnline> page = confIpDriftOnlineMapper.listPage(new Page<>(confIpDriftOnlineParams.getPageNum(), confIpDriftOnlineParams.getPageSize()), confIpDriftOnlineParams);

		// 填充系统名称,组件名称和网端名称
		Map<Integer, String> confSystemMap = confSystemService.list().stream().collect(Collectors.toMap(ConfSystem::getId, ConfSystem::getName));
		List<ConfComponentIp> confComponents = confComponentIpService.list();
		Map<Integer, String> componentIpMap = confComponents.stream().collect(Collectors.toMap(ConfComponentIp::getId, ConfComponentIp::getIp));
		Map<Integer, String> componentPortMap = confComponents.stream().collect(Collectors.toMap(ConfComponentIp::getId, ConfComponentIp::getPort));

		List<ConfSegment> confSegments = confSegmentService.list();
		Map<Integer, String> confSegmentNameMap = confSegments.stream().collect(Collectors.toMap(ConfSegment::getId, ConfSegment::getName));
		for (ConfIpDriftOnline confIpDriftOnline : page.getRecords()) {
			confIpDriftOnline.setSystemName(confSystemMap.get(confIpDriftOnline.getSystemId()));
			confIpDriftOnline.setComponentIp(componentIpMap.get(confIpDriftOnline.getIpComponentId()));
			confIpDriftOnline.setPort(componentPortMap.get(confIpDriftOnline.getIpComponentId()));
			confIpDriftOnline.setOldSegmentName(confSegmentNameMap.get(confIpDriftOnline.getOldSegmentId()));
			confIpDriftOnline.setNewSegmentName(confSegmentNameMap.get(confIpDriftOnline.getNewSegmentId()));
		}
		return page;
	}

}
